#include "desktopui.h"
#include "ui_desktopui.h"

desktopUI::desktopUI(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::desktopUI)
{
    ui->setupUi(this);
    createAction();
    createTrayIcon();

    QDesktopWidget * desktopWidget = QApplication::desktop();
    //available geometry
    //QRect deskRect = desktopWidget->availableGeometry();
    //screen geometry
    QRect screenRect = desktopWidget->screenGeometry();

    qint32 _width = screenRect.width();
    qint32 _height = screenRect.height();
    //g_nActScreenX = deskRect.width();
    //g_nActScreenY = deskRect.height();

    //initPosSize(_width,_height);

    //get screen count（screen copy ,return 1）
    //qint32 g_nScreenCount = desktopWidget->screenCount();
    initPosSize(_width,_height);

    _pCalendarUI = new Calendar(QPoint(gCalendarX,gCalendarY),QSize(gCalendarWidth,gCalendarHeight));
    _pCalendarUI->setWindowOpacity(0.8);

    _pNoteTaskUI = new NoteTask(_width,_height,_pCalendarUI->getItemMap());
    _pNoteTaskUI->setWindowOpacity(0.8);

    //embeded in desktop
//    HWND desktopHwnd = findDesktopIconWnd();
//    if(desktopHwnd)
//    {
//        SetParent(_pCalendarUI->winId(),desktopHwnd);
//        SetParent(_pNoteTaskUI->winId(),desktopHwnd);
//    }

    connect(_pCalendarUI->_pCalendarWindow->ui_editItemDialog,SIGNAL(updateDesktop()),this,SLOT(update()));

    _trayIcon->show();

}

desktopUI::~desktopUI()
{
    delete ui;
    SAFE_DELETE_POINTER(_pCalendarUI);
    SAFE_DELETE_POINTER(_pNoteTaskUI);
    SAFE_DELETE_POINTER(_trayIconMenu);
    SAFE_DELETE_POINTER(_backupDataAction);
    SAFE_DELETE_POINTER(_reloadDataAction);
    SAFE_DELETE_POINTER(_quitAction);
    SAFE_DELETE_POINTER(_trayIcon);
}

void desktopUI::initPosSize(qint32 screenWidth,qint32 screenHeigh)
{
    gNoteTaskWidth  = screenWidth/6;
    gCalendarWidth  = 820;//screenWidth/2;
    gCalendarHeight = 290;//screenHeigh/2;
    gCalendarX = screenWidth - gNoteTaskWidth - gCalendarWidth - 5;
    gCalendarY = screenHeigh/8;
}

void desktopUI::show()
{
    //qDebug()<<"desktopUI::show";
    _pCalendarUI->show();
    //qDebug()<<"desktopUI::show:_pCalendarUI";
    _pNoteTaskUI->show();
    //qDebug()<<"desktopUI::show:_pNoteTaskUI";
}

void desktopUI::update()
{
    _pItemMap = _pCalendarUI->getItemMap();
    _pNoteTaskUI->update(_pItemMap);
    _pCalendarUI->updateUI();
    //qDebug()<<"desktopUI::update _pItemMap size_2"<<_pItemMap->getSize();
}

void desktopUI::createTrayIcon()
{
    _trayIconMenu = new QMenu(this);
    _trayIconMenu->addAction(_backupDataAction);
    _trayIconMenu->addAction(_reloadDataAction);
    _trayIconMenu->addSeparator();
    _trayIconMenu->addAction(_quitAction);

    _trayIcon = new QSystemTrayIcon(this);
    _trayIcon->setContextMenu(_trayIconMenu);
    createIcon();
}

void desktopUI::createAction()
{
    _backupDataAction = new QAction(tr("&Backup Data"),this);
    connect(_backupDataAction,SIGNAL(triggered()),this,SLOT(slotBackupData()));

    _reloadDataAction = new QAction(tr("&Reload Data"),this);
    connect(_reloadDataAction,SIGNAL(triggered()),this,SLOT(slotReloadData()));

    _quitAction = new QAction(tr("&Quit"),this);
    connect(_quitAction,SIGNAL(triggered()),this,SLOT(slotQuit()));

}


void desktopUI::slotBackupData()
{
    _pItemMap = _pCalendarUI->getItemMap();
    backupDataDialog * pBackupDialog = new backupDataDialog;
    if(QDialog::Accepted == pBackupDialog->exec())
    {
        _pCalendarUI->backupData(pBackupDialog->getFileName());
    }
    else
        qDebug()<<"backupDialog rejected!";
}

void desktopUI::slotReloadData()
{
    QFileDialog fileDlg;
    fileDlg.setNameFilter(tr("Note File(*.ldc)"));
    fileDlg.setViewMode(QFileDialog::Detail);
    QStringList files;
    if(fileDlg.exec())
    {
        files = fileDlg.selectedFiles();
        QString fileName = files.at(0);
        fileName.remove(QDir::currentPath());
        fileName.remove("/");
        _pCalendarUI->reloadData(fileName);
        update();
        _pCalendarUI->backupData();
    }
}

void desktopUI::slotQuit()
{
    _pCalendarUI->accept();
    _pNoteTaskUI->accept();
}


void desktopUI::createIcon()
{
    QDate date = QDate::currentDate();
    QString str;
    qint32 day = date.day();
    str.setNum(day);

    if(day<10)
    {
        str.prepend("0");
    }

    QString path(":/images/");
    path = path + str + ".png";
    QIcon icon(path);
    _trayIcon->setIcon(icon);
    setWindowIcon(icon);
}
